package com.example.loginpage

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.navigation.fragment.findNavController
import com.example.loginpage.InputUtil.BitmapStorage
import com.example.loginpage.databinding.FragmentStoreBinding
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlin.properties.Delegates

class StoreFragment : Fragment(){
    private lateinit var binding: FragmentStoreBinding
    private lateinit var sharedPreferences: SharedPreferences
    private var storeNum by Delegates.notNull<Int>()
    private lateinit var store:MutableList<String>
    private lateinit var storeString: String
    @SuppressLint("SetTextI18n", "InflateParams")
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentStoreBinding.inflate(layoutInflater,container, false)
        sharedPreferences = requireActivity().getSharedPreferences(AccountFragment.PREFS_NAME, Context.MODE_PRIVATE)
        storeNum = sharedPreferences.getInt(AccountFragment.STORE_NUM, 1)
        storeString = sharedPreferences.getString(AccountFragment.STORE_STRING, "[]").toString()
        store = Gson().fromJson(storeString, object : TypeToken<List<String>>(){}.type)

        val iterator = store.iterator()
        while(iterator.hasNext()){
            val item = iterator.next()
            if(BitmapStorage.readPicture(item)!=null){
                val storeRecording = layoutInflater.inflate(R.layout.store_recording, null) as ViewGroup
                val storeView = storeRecording.findViewById<ImageView>(R.id.StoreView)
                val storeText = storeRecording.findViewById<TextView>(R.id.StoreText)
                val arrowView = storeRecording.findViewById<ImageView>(R.id.ArrowView)
                storeText.text = item.replace(".png", "")
                binding.allStoreRecording.addView(storeRecording)

                storeView.setOnLongClickListener {
                    Toast.makeText(requireContext(), "成功删除${item.replace(".png", "")}", Toast.LENGTH_SHORT).show()
                    BitmapStorage.deletePicture(item)
                    true
                }

                storeText.setOnLongClickListener {
                    // 创建一个LayoutInflater来加载对话框的布局
                    val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
                    // Inflate the custom layout
                    val view = layoutInflater.inflate(R.layout.custom_dialog, null)
                    // Get the EditText from the custom layout
                    val editText = view.findViewById<EditText>(R.id.editText)
                    // 创建一个AlertDialog.Builder
                    val builder = AlertDialog.Builder(requireContext())
                    builder.setView(view)
                    builder.setTitle("修改名称")
                    builder.setPositiveButton("确定") { dialog, _ ->
                        // 获取用户输入的文本并更新TextView
                        storeText.text = editText.text
                        store.add(storeText.text.toString()+".png")
                        storeString = Gson().toJson(store)
                        sharedPreferences.edit()
                            .putString(AccountFragment.STORE_STRING, storeString)
                            .commit()
                        BitmapStorage.renamePicture(item, storeText.text.toString()+".png")
                        dialog.dismiss()
                    }
                    builder.setNegativeButton("取消") { dialog, _ ->
                        dialog.dismiss()
                    }
                    // 显示对话框
                    builder.show()
                    true
                }
                arrowView.setOnClickListener {
                    val navCtl = findNavController()
                    val bundle = Bundle()
                    bundle.putString("imageData", storeText.text.toString()+".png")
                    navCtl.navigate(R.id.ImageFragment, bundle)
                }
            } else{
                iterator.remove()
                storeString = Gson().toJson(store)
                sharedPreferences.edit()
                    .putString(AccountFragment.STORE_STRING, storeString)
                    .apply()
            }
        }
        return binding.root
    }

}